package com.yunsuo.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.sql.*;

public class ModifyKey {
    private static Connection getConn() {
        String driver = "com.mysql.cj.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/raspberry?characterEncoding=UTF-8";
        String username = "root";
        String password = "123456";
        Connection conn = null;
        try {
            Class.forName(driver); //classLoader,加载对应驱动
            conn = (Connection) DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    public static Integer getAll(String u_id,int l_id,String keyInfo) {
        JSONObject jsonObject1 = JSON.parseObject(keyInfo);
        String password=jsonObject1.getString("kPassword");
        //System.out.println("password="+password);
        Connection conn = getConn();
        String sql = "select * from key_info_raspberry where k_lock_id="+l_id+" and k_user_id='"+
                u_id+"' and k_available_times=-1";
        PreparedStatement pstmt;
        try {
            pstmt = (PreparedStatement)conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            int col = rs.getMetaData().getColumnCount();
            //System.out.println("============================");
            if(rs.next()){
                if(modify(u_id,l_id,password)==1){
                    //System.out.println("密码修改成功");
                }else{
                    //System.out.println("密码修改失败");
                }
            }else{
                //System.out.println("未查找到密码信息,开始添加密码");
                if(insert(keyInfo)==1){
                    //System.out.println("密码添加成功");
                }
            }
            //System.out.println("============================");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static int modify(String u_id,int l_id,String password) {
        Connection conn = getConn();
        int i = 0;
        String sql = "update key_info_raspberry set k_password='" + password + "' where k_user_id='"
                + u_id + "'"+" and k_lock_id="+l_id+" and k_available_times=-1";
        PreparedStatement pstmt;
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            i = pstmt.executeUpdate();
            //System.out.println("resutl: " + i);
            pstmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    public static int insert(String keyInfo) {
//        params.put("result", "ok");
//        params.put("kPassword",keyInfos.get(0).getkPassword());
//        params.put("kId",keyInfos.get(0).getkId().toString());
//        params.put("kUuid",keyInfos.get(0).getkUuid());
//        params.put("kFailureTime",keyInfos.get(0).getkFailureTime());
//        params.put("kAvailableTimes",keyInfos.get(0).getkAvailableTimes().toString());
//        params.put("kDelete",keyInfos.get(0).getkDelete().toString());
//        params.put("kIsModify",keyInfos.get(0).getkIsModify().toString());
//        params.put("kLockId",keyInfos.get(0).getkLockId().toString());
//        params.put("kUserId",keyInfos.get(0).getkUserId().toString());
        JSONObject jsonObject1 = JSON.parseObject(keyInfo);
        String password=jsonObject1.getString("kPassword");
        //String kId=jsonObject1.getString("kId");
        String kUuid=jsonObject1.getString("kUuid");
        String kFailureTime=jsonObject1.getString("kFailureTime");
        String kAvailableTimes=jsonObject1.getString("kAvailableTimes");
        String kDelete=jsonObject1.getString("kDelete");
        String kIsModify=jsonObject1.getString("kIsModify");
        String kLockId=jsonObject1.getString("kLockId");
        String kUserId=jsonObject1.getString("kUserId");
        Connection conn = getConn();
        int i = 0;
        String sql = "insert into key_info_raspberry (k_uuid,k_lock_id,k_user_id,k_password,k_available_times," +
                "k_is_modify,k_delete,k_failure_time) values(?,?,?,?,?,?,?,?)";

        PreparedStatement pstmt;
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            pstmt.setString(1, kUuid);
            pstmt.setInt(2, Integer.parseInt(kLockId));
            pstmt.setInt(3, Integer.parseInt(kUserId));
            pstmt.setString(4, password);
            pstmt.setInt(5, Integer.parseInt(kAvailableTimes));
            pstmt.setInt(6, 0);
            pstmt.setInt(7, Integer.parseInt(kDelete));
            pstmt.setString(8, kFailureTime);
            i = pstmt.executeUpdate();
            pstmt.close();
            conn.close();
            //System.out.println("11111");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }


    public static int del(String keyInfo) {
        JSONObject jsonObject1 = JSON.parseObject(keyInfo);
        String k_uuid=jsonObject1.getString("kUuid");
        Connection conn = getConn();
        int i = 0;
        String sql = "update key_info_raspberry set k_delete=1 where k_uuid='"
                + k_uuid+"'";
        PreparedStatement pstmt;
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            i = pstmt.executeUpdate();
            //System.out.println("resutl: " + i);
            pstmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

}
